//
//  BQTopGuideView.m
//  BeeQuick
//
//  Created by mac on 16/11/21.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQTopGuideView.h"

#import "iflyMSC/IFlyRecognizerViewDelegate.h"
#import "iflyMSC/IFlyRecognizerView.h"

#import "iflyMSC/IFlyMSC.h"
#import "ISRDataHelper.h"
@interface BQTopGuideView()<IFlyRecognizerViewDelegate>


@property (nonatomic,weak)UIView *containerView;

@property (nonatomic,weak)UIButton *titleBtn;

@property (nonatomic,weak)UIButton *scancodeBtn;

@property (nonatomic,weak)UIButton *searchBtn;

@property (nonatomic,weak)UIView *titleView;

@property (nonatomic,weak)UILabel *titleLabel;


@end
@implementation BQTopGuideView{
    
    IFlyRecognizerView      *_iflyRecognizerView;
    
}

- (instancetype)init
{
    if(self = [super init])
    {
        [self setupUI];
        
        NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"58350fc1"];

        
        [IFlySpeechUtility createUtility:initString];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = [UIColor clearColor];
    
    UIView *containerView = [[UIView alloc] init];
    
    self.containerView = containerView;
    
    containerView.backgroundColor = [UIColor cz_colorWithHex:0xffd600];
    
    [self addSubview:containerView];
    
    UIView *titleView = [[UIView alloc] init];
    
    self.titleView = titleView;
    
    [self insertSubview:titleView aboveSubview:containerView];
    
    UILabel *titleLabel = [UILabel cz_labelWithText:@"配送至:" fontSize:10 color:[UIColor whiteColor]];
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel = titleLabel;
    
    titleView.layer.cornerRadius = 10.0;
    
    titleView.clipsToBounds = YES;
    
    [titleView addSubview:titleLabel];
    
    //titleBtn
//    UIButton *titleBtn = [UIButton cz_textButton:@"点击此处定位" fontSize:8 normalColor:[UIColor whiteColor] selectedColor:[UIColor whiteColor]];
//    
//    titleBtn.layer.borderColor = [UIColor clearColor].CGColor;
//    
//    [titleBtn addTarget:self action:@selector(clickTitleBtn:) forControlEvents:UIControlEventTouchUpInside];
//    
//    self.titleBtn = titleBtn;
//    
//    titleBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
//    
//    titleBtn.layer.borderWidth = 0.5;
//    
//    titleBtn.layer.cornerRadius = 10.0;
//    
//    titleBtn.clipsToBounds = YES;
//    
//    titleBtn.titleLabel.font = [UIFont systemFontOfSize:13 weight:3];
//    
//    titleBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//    
//    [titleView addSubview:titleBtn];
    
    //搜索
    UIButton *searchBtn = [[UIButton alloc] init];
    
    self.searchBtn = searchBtn;
    
    searchBtn.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
    searchBtn.layer.cornerRadius = 15.0;
    
    searchBtn.clipsToBounds = YES;
    
    [searchBtn addTarget:self action:@selector(clickSearchBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self insertSubview:searchBtn aboveSubview:containerView];
    
    //扫一扫
    UIButton *scancodeBtn = [[UIButton alloc] init];
    
    self.scancodeBtn = scancodeBtn;
    
    [scancodeBtn addTarget:self action:@selector(clickScancodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    scancodeBtn.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
    scancodeBtn.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
    
    scancodeBtn.layer.cornerRadius = 15.0;
    
    scancodeBtn.clipsToBounds = YES;
    
    [self addSubview:scancodeBtn];
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(8);
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(230, 30));
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.left.offset(5);
    }];
    
//    [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(titleLabel.mas_right);
//        make.centerY.equalTo(titleView);
//    }];
    
    //    [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.equalTo(self).offset(8);
    //        make.centerX.equalTo(self);
    //        make.size.mas_equalTo(CGSizeMake(160, 30));
    //    }];
    
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(8);
        make.right.offset(-8);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [scancodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(8);
        make.left.offset(8);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    
    
    [self setupTextField];
}



#pragma mark - 扫一扫
- (void)clickScancodeBtn:(UIButton *)sender
{
    if ([_delegate respondsToSelector:@selector(pushToScancodeController)]) {
        [_delegate pushToScancodeController];
    }
}



#pragma mark - 搜索
- (void)clickSearchBtn:(UIButton *)sender
{
    //    NSLog(@"搜索");
    
    if ([_delegate respondsToSelector:@selector(presentSearchViewController)]) {
        
        [_delegate presentSearchViewController];
    }
}


#pragma mark - 调整颜色,图片,透明度
- (void)setContainerViewAlpha:(CGFloat)alpha
{
    
    self.containerView.alpha = alpha;
    
    UIColor *titleColor = alpha > 1 ? [UIColor blackColor] : [UIColor whiteColor];
    
    [self.titleBtn setTitleColor:titleColor forState:UIControlStateNormal];
    
    UIImage *scancodeImage = alpha > 1 ? [UIImage imageNamed:@"icon_black_scancode"] : [UIImage imageNamed:@"icon_white_scancode"];
    
    [self.scancodeBtn setImage:scancodeImage forState:UIControlStateNormal];
    
    UIImage *searchImage = alpha > 1 ? [UIImage imageNamed:@"icon_search"] : [UIImage imageNamed:@"icon_search_white"];
    
    self.scancodeBtn.backgroundColor = alpha > 1 ? [UIColor clearColor] : [UIColor colorWithWhite:0.5 alpha:0.5];
    
    [self.searchBtn setImage:searchImage forState:UIControlStateNormal];
    
    self.searchBtn.backgroundColor = alpha > 1 ? [UIColor clearColor] : [UIColor colorWithWhite:0.5 alpha:0.5];
    
    self.alpha = alpha < 0 ? 0.5 + alpha : 1;
    
    self.titleView.backgroundColor = alpha > 1 ? [UIColor clearColor] : [UIColor colorWithWhite:0.5 alpha:0.5];
    
    self.titleLabel.textColor = alpha > 1 ? [UIColor blackColor] : [UIColor whiteColor];
    
    
}




#pragma mark - 定位的按钮
- (void)clickTitleBtn:(UIButton*)sender
{
    if([self.delegate respondsToSelector:@selector(pushToLocaling)])
    {
        [self.delegate pushToLocaling];
    }
}


- (void)setupTextField
{
    UIView *v = [[UIView alloc]init];
    v.frame =  CGRectMake(40, 0, 190, 30);
    [_titleView addSubview:v];
//    v.backgroundColor = [UIColor yellowColor];
    
    UITextField *textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(0, 0, 160, 30);
    textField.font = [UIFont systemFontOfSize:12];
//    textField.backgroundColor = [UIColor redColor];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    NSDictionary *dict = @{NSForegroundColorAttributeName:[UIColor yellowColor]};
    NSAttributedString *str = [[NSAttributedString alloc]initWithString:@"请输入地址" attributes:dict];
    textField.attributedPlaceholder = str;
    [v addSubview:textField];
    _textField = textField;
    
    
    UIButton *voiceBtn = [[UIButton alloc] init];
    [voiceBtn setImage:[UIImage imageNamed:@"chat_bottom_voice_nor"] forState:UIControlStateNormal];
    [voiceBtn setImage:[UIImage imageNamed:@"chat_bottom_voice_press"] forState:UIControlStateHighlighted];
    voiceBtn.frame = CGRectMake(160,0, 30, 30);
    [voiceBtn addTarget:self action:@selector(clickVoiceBtn) forControlEvents:UIControlEventTouchUpInside];
    [v addSubview:voiceBtn];
    
    
    //初始化语音识别控件
    _iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:CGPointMake([UIScreen mainScreen].bounds.size.width * 0.5, [UIScreen mainScreen].bounds.size.height * 0.5)];
    _iflyRecognizerView.delegate = self;
    [_iflyRecognizerView setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];
    //asr_audio_path保存录音文件名，如不再需要，设置value为nil表示取消，默认目录是documents
    [_iflyRecognizerView setParameter:@"asrview.pcm " forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
}

// 开始识别  输入地址
- (void)clickVoiceBtn
{
    [_iflyRecognizerView start];
}




#pragma mark  IFlyRecognizerViewDelegate
/*识别结果返回代理
 @param resultArray 识别结果
 @ param isLast 表示是否最后一次结果
 */
- (void)onResult: (NSArray *)resultArray isLast:(BOOL) isLast
{
    
    [_iflyRecognizerView cancel]; //取消识别
    
    
    NSMutableString *resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = resultArray[0];
    for (NSString *key in dic) {
        [resultString appendFormat:@"%@",key];
    }
    
    //    _result =[NSString stringWithFormat:@"%@%@", _addressField.text,resultString];
    
    NSString * resultFromJson =  [ISRDataHelper stringFromJson:resultString];
    //    NSLog(@"%@",resultFromJson);
    
    
    NSLog(@"%@",resultFromJson);
    
    _textField.text = resultFromJson;
}

@end
